package utils;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;

import pojo.TableAttributeDTO;
import pojo.TransmessionVO;
import pojo.UserInfoDTO;
import pojo.Ze42DTO;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonUtils {
	public static void jsonLeaf(JsonNode node) {
		if (node.isValueNode()) {
			return;
		}

		if (node.isObject()) {
			Iterator<Entry<String, JsonNode>> it = node.fields();
			while (it.hasNext()) {
				Entry<String, JsonNode> entry = it.next();
				System.out.println(entry.getKey() + ":" + entry.getValue());
				jsonLeaf(entry.getValue());
			}
		}

		if (node.isArray()) {
			Iterator<JsonNode> it = node.iterator();
			while (it.hasNext()) {
				jsonLeaf(it.next());
			}
		}
	}

	public static void readJson(String jsondata) {
		try {
			ObjectMapper jackson = new ObjectMapper();
			JsonNode node = jackson.readTree(jsondata);
			jsonLeaf(node);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static Ze42DTO createData(){
		Ze42DTO ze42dto = new Ze42DTO();
		String url = "D:/develop_study/apache-tomcat-7.0.73-x64/webapps/PageOfficeTest/doc/template.doc";
		ze42dto.setDocumentURL(url);
		TransmessionVO vo = new TransmessionVO();
		vo.setNowDate(new Date());
		vo.setNumber(1L);
		vo.setSpecialMode("0");
		//表格属性
		TableAttributeDTO attributeDTO = new TableAttributeDTO();
		attributeDTO.setStartRowNumber(2);
		attributeDTO.setTotalColumnNumber(3);
		List<TableAttributeDTO> tableAttributes = new ArrayList<TableAttributeDTO>();
		tableAttributes.add(attributeDTO);
		vo.setTableAttributes(tableAttributes);
		//表格数据
		UserInfoDTO userInfo = new UserInfoDTO();
		userInfo.setAac003("张三");
		userInfo.setAac004("1");
		userInfo.setAge(18);
		userInfo.setBirthDate(new Date());
		
		UserInfoDTO userInfo2 = new UserInfoDTO();
		userInfo2.setAac003("李四");
		userInfo2.setAac004("2");
		userInfo2.setAge(19);
		userInfo2.setBirthDate(new Date());
		List<UserInfoDTO>  infoDTOs = new ArrayList<UserInfoDTO>();
		infoDTOs.add(userInfo);
		infoDTOs.add(userInfo2);
		vo.setDtos(infoDTOs);
		
		ObjectMapper mapper = new ObjectMapper();
        String mapJakcson;
		try {
			mapJakcson = mapper.writeValueAsString(vo);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
			mapJakcson = "";
		}
		ze42dto.setJsonData(mapJakcson);
		return ze42dto;
	}
	
	public static void main(String[] args) {
		//TransmessionVO transmessionVO = new TransmessionVO();
		//readJson(transmessionVO.getJsondata());
		Ze42DTO ze42dto = new Ze42DTO();
		String url = "D:/develop_study/apache-tomcat-7.0.73-x64/webapps/PageOfficeTest/doc/template.doc";
		ze42dto.setDocumentURL(url);
		TransmessionVO vo = new TransmessionVO();
		vo.setNowDate(new Date());
		vo.setNumber(1L);
		vo.setSpecialMode("0");
		//表格属性
		TableAttributeDTO attributeDTO = new TableAttributeDTO();
		attributeDTO.setStartRowNumber(2);
		attributeDTO.setTotalColumnNumber(3);
		List<TableAttributeDTO> tableAttributes = new ArrayList<TableAttributeDTO>();
		tableAttributes.add(attributeDTO);
		vo.setTableAttributes(tableAttributes);
		//表格数据
		UserInfoDTO userInfo = new UserInfoDTO();
		userInfo.setAac003("张三");
		userInfo.setAac004("1");
		userInfo.setAge(18);
		userInfo.setBirthDate(new Date());
		
		UserInfoDTO userInfo2 = new UserInfoDTO();
		userInfo2.setAac003("李四");
		userInfo2.setAac004("2");
		userInfo2.setAge(19);
		userInfo2.setBirthDate(new Date());
		List<UserInfoDTO>  infoDTOs = new ArrayList<UserInfoDTO>();
		infoDTOs.add(userInfo);
		infoDTOs.add(userInfo2);
		vo.setDtos(infoDTOs);
		
		ObjectMapper mapper = new ObjectMapper();
        String mapJakcson;
		try {
			mapJakcson = mapper.writeValueAsString(vo);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
			mapJakcson = "";
		}
		ze42dto.setJsonData(mapJakcson);
        System.out.println(mapJakcson);
	}
}
